import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('../views/login/Login')
const Home = () => import('../views/home/Home')
const Welcome = () => import('../components/welcome/Welcome')
const User = () => import('../components/user/User')
const ComputePass = () => import('../components/computePass/ComputePass')
const Health = () => import('../components/health/Health')
const Outs = () => import('../components/pass/Outs')
const Enter = () => import('../components/pass/Enter')
const Check = () => import('../components/check/Check')

Vue.use(VueRouter)

  const routes = [
    {
      path: '/',
      redirect: '/login'
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/home',
      component: Home,
      redirect: '/welcome',
      children: [
        {path: '/welcome', component :Welcome},
        {path: '/user', component: User},
        {path: '/computePass', component: ComputePass},
        {path: '/health', component: Health},
        {path: '/outs', component: Outs},
        {path: '/enter', component: Enter},
        {path: '/check', component: Check}
      ]
    }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login') return next()
  // 获取token登录状态
  const tokenStr = window.sessionStorage.getItem('token')
  //未登录，返回登录界面
  if (!tokenStr) return next('/login')
  next()
})

export default router
